/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.persistence.ui.wizard.page;

import net.jonbuck.tassoo.persistence.dao.TassooDao;
import net.jonbuck.tassoo.persistence.dao.TassooDaoImpl;
import net.jonbuck.tassoo.persistence.ui.wizard.model.NewTaskStore;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.wizard.WizardPageSupport;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Group;

/**
 * 
 * 
 * @since 1.0.0
 */
public class TaskStoreDetailsPage extends WizardPage {

	private DataBindingContext m_bindingContext;
	public static final String PAGE_NAME = "net.jonbuck.tassoo.wizard.page.taskstoredetails"; //$NON-NLS-1$
	private Text txtTaskStoreName;
	private Text locationText;
	private Label locationLabel;
	private Button btnBrowseLocation;
	private TassooDao tassooDao = TassooDaoImpl.getInstance();
	private NewTaskStore newTaskStore;
	private Label lblTaskStoreName;
	private Button btnDefaultLocation;
	private Group grpTaskStoreDetails;
	private Label lblDescription;
	private Text txtDescription;

	/**
	 * 
	 * @param pageName
	 */
	public TaskStoreDetailsPage(String pageName, NewTaskStore newTaskStore) {
		super(pageName);
		setTitle("Task Store");
		this.newTaskStore = newTaskStore;
	}

	/**
	 * 
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout gl_container = new GridLayout();
		gl_container.verticalSpacing = 10;
		container.setLayout(gl_container);
		setControl(container);

		Composite taskNameComposite = new Composite(container, SWT.NONE);
		taskNameComposite.setLayout(new GridLayout(2, false));
		taskNameComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		lblTaskStoreName = new Label(taskNameComposite, SWT.NONE);
		lblTaskStoreName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblTaskStoreName.setText("Task Store Name:");

		txtTaskStoreName = new Text(taskNameComposite, SWT.BORDER);
		txtTaskStoreName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Composite taskLocationComposite = new Composite(container, SWT.NONE);
		taskLocationComposite.setLayout(new GridLayout(3, false));
		taskLocationComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		btnDefaultLocation = new Button(taskLocationComposite, SWT.CHECK);
		btnDefaultLocation.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1));
		btnDefaultLocation.setText("Use Default Location");
		btnDefaultLocation.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (btnDefaultLocation.getSelection()) {
					locationLabel.setEnabled(false);
					locationText.setEnabled(false);
					locationText.setText(calculateLocationText());
					btnBrowseLocation.setEnabled(false);
				} else {
					locationLabel.setEnabled(true);
					locationText.setEnabled(true);
					locationText.setText(StringUtils.EMPTY);
					btnBrowseLocation.setEnabled(true);
				}
			}
		});

		locationLabel = new Label(taskLocationComposite, SWT.NONE);
		locationLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		locationLabel.setText("Location:");
		locationLabel.setEnabled(false);

		locationText = new Text(taskLocationComposite, SWT.BORDER);
		locationText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		btnBrowseLocation = new Button(taskLocationComposite, SWT.NONE);
		GridData gd_btnBrowseLocation = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_btnBrowseLocation.widthHint = 80;
		btnBrowseLocation.setLayoutData(gd_btnBrowseLocation);
		btnBrowseLocation.setText("Browse");
		btnBrowseLocation.setEnabled(false);
		btnBrowseLocation.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dirDialog = new DirectoryDialog(Display.getDefault().getActiveShell());
				if (StringUtils.isNotBlank(dirDialog.open())) {
					locationText.setText(dirDialog.getFilterPath());
				}
			}
		});

		grpTaskStoreDetails = new Group(container, SWT.NONE);
		grpTaskStoreDetails.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		grpTaskStoreDetails.setText("Task Store Details");
		GridLayout gl_grpTaskStoreDetails = new GridLayout();
		grpTaskStoreDetails.setLayout(gl_grpTaskStoreDetails);

		lblDescription = new Label(grpTaskStoreDetails, SWT.NONE);
		lblDescription.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		lblDescription.setText("Description:");

		txtDescription = new Text(grpTaskStoreDetails, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		GridData gd_txtDescription = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
		gd_txtDescription.heightHint = 40;
		txtDescription.setLayoutData(gd_txtDescription);

		m_bindingContext = initDataBindings();
		initBindingSupport();
		populateFields();
		txtTaskStoreName.setFocus();
	}

	/**
	 * 
	 */
	private void initBindingSupport() {
		WizardPageSupport.create(this, m_bindingContext);
	}

	/**
	 * 
	 * @return
	 */
	protected String calculateLocationText() {
		String returnValue = null;
		if (newTaskStore.isSystemDir()) {
			if (StringUtils.isBlank(newTaskStore.getTaskStoreName())) {
				returnValue = tassooDao.getSystemDir();
			} else {
				returnValue = tassooDao.getSystemDir().concat(newTaskStore.getTaskStoreName());
			}
		} else {
			returnValue = StringUtils.EMPTY;
		}
		return returnValue;
	}

	/**
	 * 
	 */
	private void populateFields() {
		locationText.setEnabled(false);
		locationText.setText(calculateLocationText());
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue txtTaskStoreNameObserveTextObserveWidget = SWTObservables.observeText(txtTaskStoreName, SWT.Modify);
		IObservableValue newTaskStoreTaskStoreNameObserveValue = PojoObservables.observeValue(newTaskStore, "taskStoreName");
		UpdateValueStrategy nameStrategy = new UpdateValueStrategy();
		nameStrategy.setBeforeSetValidator(new IValidator() {
			public IStatus validate(Object value) {
				String taskStoreName = (String) value;
				if (StringUtils.isBlank(taskStoreName)) {
					return ValidationStatus.error("Task Store Name needs to be populated");
				}
				return ValidationStatus.ok();
			}
		});
		bindingContext.bindValue(txtTaskStoreNameObserveTextObserveWidget, newTaskStoreTaskStoreNameObserveValue, nameStrategy,
				null);
		//
		IObservableValue btnDefaultLocationObserveSelectionObserveWidget = SWTObservables.observeSelection(btnDefaultLocation);
		IObservableValue newTaskStoreSystemDirObserveValue = PojoObservables.observeValue(newTaskStore, "systemDir");
		bindingContext.bindValue(btnDefaultLocationObserveSelectionObserveWidget, newTaskStoreSystemDirObserveValue, null, null);
		//
		IObservableValue locationTextObserveTextObserveWidget = SWTObservables.observeText(locationText, SWT.Modify);
		IObservableValue newTaskStoreFilePathObserveValue = PojoObservables.observeValue(newTaskStore, "filePath");
		UpdateValueStrategy locationStrategy = new UpdateValueStrategy();
		locationStrategy.setBeforeSetValidator(new IValidator() {
			public IStatus validate(Object value) {
				String taskStoreLocation = (String) value;
				if (StringUtils.isBlank(taskStoreLocation)) {
					return ValidationStatus.error("A location needs to be specified for the task store");
				}
				return ValidationStatus.ok();
			}
		});
		bindingContext.bindValue(locationTextObserveTextObserveWidget, newTaskStoreFilePathObserveValue, locationStrategy, null);
		//
		IObservableValue txtDescriptionObserveTextObserveWidget = SWTObservables.observeText(txtDescription, SWT.Modify);
		IObservableValue newTaskStoreDescriptionObserveValue = PojoObservables.observeValue(newTaskStore, "description");
		bindingContext.bindValue(txtDescriptionObserveTextObserveWidget, newTaskStoreDescriptionObserveValue, null, null);
		//
		return bindingContext;
	}
}
